#pragma once
#include "uart/ProtocolSender.h"
/*
*此文件由GUI工具生成
*文件功能：用于处理用户的逻辑相应代码
*功能说明：
*========================onButtonClick_XXXX
当页面中的按键按下后系统会调用对应的函数，XXX代表GUI工具里面的[ID值]名称，
如Button1,当返回值为false的时候系统将不再处理这个按键，返回true的时候系统将会继续处理此按键。比如SYS_BACK.
*========================onSlideWindowItemClick_XXXX(int index) 
当页面中存在滑动窗口并且用户点击了滑动窗口的图标后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如slideWindow1;index 代表按下图标的偏移值
*========================onSeekBarChange_XXXX(int progress) 
当页面中存在滑动条并且用户改变了进度后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如SeekBar1;progress 代表当前的进度值
*========================ogetListItemCount_XXXX() 
当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表的总数目,XXX代表GUI工具里面的[ID值]名称，
如List1;返回值为当前列表的总条数
*========================oobtainListItemData_XXXX(ZKListView::ZKListItem *pListItem, int index)
 当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表当前条目下的内容信息,XXX代表GUI工具里面的[ID值]名称，
如List1;pListItem 是贴图中的单条目对象，index是列表总目的偏移量。具体见函数说明
*========================常用接口===============
*LOGD(...)  打印调试信息的接口
*mTextXXXPtr->setText("****") 在控件TextXXX上显示文字****
*mButton1Ptr->setSelected(true); 将控件mButton1设置为选中模式，图片会切换成选中图片，按钮文字会切换为选中后的颜色
*mSeekBarPtr->setProgress(12) 在控件mSeekBar上将进度调整到12
*mListView1Ptr->refreshListView() 让mListView1 重新刷新，当列表数据变化后调用
*mDashbroadView1Ptr->setTargetAngle(120) 在控件mDashbroadView1上指针显示角度调整到120度
*
* 在Eclipse编辑器中  使用 “alt + /”  快捷键可以打开智能提示
*/

#include "utils/GpioHelper.h"
#include "utils/TimeHelper.h"
#include "utils/BrightnessHelper.h"
#include "utils/TimeHelper.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "os/MountMonitor.h"
#include "utils/BrightnessHelper.h"
#include "utils/GpioHelper.h"
#include "os/UpgradeMonitor.h"
#include "net/NetManager.h"
#include "manager/ConfigManager.h"
#include "storage/StoragePreferences.h"

BYTE updateTemp = 0;
extern bool syncWeather();
static void updateUI_time() {
    char timeStr[64];
    struct tm *t = TimeHelper::getDateTime();

    static const char *day[] = { "日", "一", "二", "三", "四", "五", "六" };
    sprintf(timeStr, "%02d:%02d 星期%s",  t->tm_hour,t->tm_min,day[t->tm_wday]);
    mTextViewRTCPtr->setText(timeStr); // 注意修改控件名称
}

static int targetPos,curPos;

/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	{0,  10}, //定时器id=0, 时间间隔6秒
	{1,  1000},
};

/**
 * 当界面构造时触发
 */
static void onUI_init(){
    //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");

	targetPos = 0;
	curPos = mWindow1Ptr->getPosition().mLeft;
	getProtocolData().wallpaperid = StoragePreferences::getInt("wallpaper",0);
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {

	switch(getProtocolData().wallpaperid){
	case 1:
		mWindow1Ptr->setBackgroundPic(CONFIGMANAGER->getResFilePath("main2/bg2.png").c_str());
		break;
	case 2:
		mWindow1Ptr->setBackgroundPic(CONFIGMANAGER->getResFilePath("main2/bg3.png").c_str());
		break;
	case 3:
		mWindow1Ptr->setBackgroundPic(CONFIGMANAGER->getResFilePath("main2/bg4.png").c_str());
		break;
	default:
		mWindow1Ptr->setBackgroundPic(CONFIGMANAGER->getResFilePath("main2/bg1.png").c_str());
		break;
	}

}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool bSyncTimer= false;
extern bool syncNetTime();

#define sub(x,y) ((x>y)?(x-y):(y-x))
static bool onUI_Timer(int id){
	static int timerfilter = 0;
	switch (id) {
	case 0:
		if(targetPos != curPos){
			//LOGD("targetPos %d,curPos:%d",targetPos,curPos);
			if(sub(targetPos,curPos) > 5){
				if(targetPos > curPos){
					curPos += (targetPos-curPos)/2;
					LayoutPosition layout = mWindow1Ptr->getPosition();
					layout.mLeft = curPos;
					mWindow1Ptr->setPosition(layout);
				}else{
					curPos -= (curPos-targetPos)/2;
					LayoutPosition layout = mWindow1Ptr->getPosition();
					layout.mLeft = curPos;
					mWindow1Ptr->setPosition(layout);
				}
			}else{
				LayoutPosition layout = mWindow1Ptr->getPosition();
				curPos = targetPos;
				layout.mLeft = curPos;
				mWindow1Ptr->setPosition(layout);
			}
		}
		break;
		case 1:
		if(!bSyncTimer){
			if(NETMANAGER->getWifiManager()->isConnected()){
				bSyncTimer = syncNetTime();
			}
		}
		updateUI_time();
		timerfilter++;
		if(timerfilter > 2*60*60){
			timerfilter = 0;
			syncWeather();
			mTextViewtempPtr->setText(getProtocolData().weather.temputre);
		}
		if(curPos == -480){
			if(updateTemp&0x80){
				updateTemp &= 0x7f;
				yh_air_settemp(41, updateTemp&0x3f);
			}
		}
		break;
		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static int downx,lastx;
static bool bmove = false;
static long downtime = 0;
static int lastpage = 0;
static bool onmain2ActivityTouchEvent(const MotionEvent &ev) {
	//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			downx = ev.mX;
			//if(downx > 400){
				bmove = true;
				lastx = ev.mX;
				targetPos = mWindow1Ptr->getPosition().mLeft;
				lastpage = targetPos/480;
				LOGD("lastpage :%d\n",lastpage);
				downtime = ev.mEventTime;
			//}
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			if(bmove){
				targetPos = targetPos - (lastx - ev.mX);
				lastx = ev.mX;
//				if(targetPos>200){
//					targetPos = 200;
//				}else if(targetPos < (-960-200)){
//					targetPos = (-960-200);
//				}
				if(targetPos>0){
					targetPos = 0;
				}else if(targetPos < (-960)){
					targetPos = (-960);
				}
			}
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			if(bmove){
				bmove = false;
				float speed  =((downx-lastx)*1.0)/((ev.mEventTime-downtime)/1000.0);
				//LOGD("speed is:%f\n",speed);
				if(speed > 800){
					//fast move;
					if(lastpage == 0){
						targetPos = -480;
					}else if(lastpage == -1){
						targetPos = -960;
					}else{
						targetPos = -960;
					}
					break;
				}else if(speed < -800){
					//fast move;
					if(lastpage == 0){
						targetPos = 0;
					}else if(lastpage == -1){
						targetPos = 0;
					}else{
						targetPos = -480;
					}
					break;
				}else{
					int dx = (downx-lastx);
					LOGD("dx is:%d\n",dx);
					if(lastpage == 0){
						if(dx > 170){
							targetPos = -480;
						}else{
							targetPos = 0;
						}
					}else if(lastpage == -1){
						if(dx < -170){
							targetPos = 0;
						}else if(dx > 170){
							targetPos = -960;
						}else{
							targetPos = -480;
						}
					}else{
						if(dx < -170){
							targetPos = -480;
						}else{
							targetPos = -960;
						}
					}
				}
			}
			break;
		default:
			break;
	}
	return false;
}
static bool onButtonClick_ButtonL1(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonL1 !!!\n");
    pButton->setSelected(!pButton->isSelected());
	GpioHelper::output("PIN8",pButton->isSelected());
	//pButton->setTextColor(pButton->isSelected()?0xffffff:0);
    return false;
}

static bool onButtonClick_ButtonL2(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonL2 !!!\n");
    pButton->setSelected(!pButton->isSelected());
	GpioHelper::output("PIN7",pButton->isSelected());

	//pButton->setTextColor(pButton->isSelected()?0xffffff:0);
    return false;
}
static bool onButtonClick_Buttonchk(ZKButton *pButton) {
    LOGD(" ButtonClick Buttonchk !!!\n");
    UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd/");
    return false;
}
static bool onButtonClick_Button1(ZKButton *pButton) {
    LOGD(" ButtonClick Button1 !!!\n");
    return false;
}
static bool onButtonClick_ButtonWiFi(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonWiFi !!!\n");

	EASYUICONTEXT->openActivity("NetSettingActivity");
    return false;
}
const char* ListviewSetupChar[]={
		"网络设置",
		"版本信息",
		"在线升级",
		"测试蓝牙",
		"壁纸"
};
static int getListItemCount_ListViewSetup(const ZKListView *pListView) {
    //LOGD("getListItemCount_ListViewSetup !\n");
    return 4;
}

static void obtainListItemData_ListViewSetup(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ ListViewSetup  !!!\n");
	pListItem->setText(ListviewSetupChar[index]);
}

static void onListItemClick_ListViewSetup(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ ListViewSetup  !!!\n");
	if(index == 2){
	    UpgradeMonitor::getInstance()->checkUpgradeFile("/mnt/extsd/");
	}else if(index == 0){
		EASYUICONTEXT->openActivity("NetSettingActivity");
	}else if(index == 3){
		EASYUICONTEXT->openActivity("mainActivity");
	}else if(index == 4){
		EASYUICONTEXT->openActivity("setwallpaperActivity");
	}
}

static const char* TempTab[]={
		"  ",
		"Lo",
		"16",
		"17",
		"18",
		"19",
		"20",
		"21",
		"22",
		"23",
		"24",
		"25",
		"26",
		"27",
		"28",
		"29",
		"Hi",
		" "
};


static int getListItemCount_ListView1(const ZKListView *pListView) {
    //LOGD("getListItemCount_ListView1 !\n");
    return sizeof(TempTab)/sizeof(char*);
}

static void obtainListItemData_ListView1(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ ListView1  !!!\n");
	//LOGD(" obtainListItemData_ ListviewTempture  !!!\n");
	//static int tempMark = temp;
	pListItem->setText(TempTab[index]);
	int first = pListView->getFirstVisibleItemIndex();
	if(index == first+1){
		pListItem->setSelected(true);
		if(updateTemp != (index+14)){
			updateTemp = (index+14)|0x80;
		}
	}else{
		pListItem->setSelected(false);
	}
}

static void onListItemClick_ListView1(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ ListView1  !!!\n");
	pListView->setSelection(index-1);
}

static bool onButtonClick_Button2(ZKButton *pButton) {
    LOGD(" ButtonClick Button2 !!!\n");
    return false;
}

static bool onButtonClick_Button3(ZKButton *pButton) {
    LOGD(" ButtonClick Button3 !!!\n");
    return false;
}

static bool onButtonClick_Button4(ZKButton *pButton) {
    LOGD(" ButtonClick Button4 !!!\n");
    return false;
}

static bool onButtonClick_Button5(ZKButton *pButton) {
    LOGD(" ButtonClick Button5 !!!\n");
    return false;
}

static bool onButtonClick_Button6(ZKButton *pButton) {
    LOGD(" ButtonClick Button6 !!!\n");
    return false;
}
static bool onButtonClick_Button7(ZKButton *pButton) {
    LOGD(" ButtonClick Button7 !!!\n");
    pButton->setSelected(!pButton->isSelected());
    if(pButton->isSelected()){
    	//yh_sence(128);
    	yh_lamp_temp(1,10,5);
    }else{
    	//yh_sence(129);
    	yh_lamp_temp(1,1,1);
    }

    return false;
}
static bool onButtonClick_ButtonAC_Open(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAC_Open !!!\n");
    pButton->setSelected(!pButton->isSelected());
    if(pButton->isSelected()){
        yh_air_open(41);
        pButton->setText("关机");
    }else{
        yh_air_close(41);
        pButton->setText("开机");
        mButtonAC_autoPtr->setSelected(false);
        mButtonAC_HeatPtr->setSelected(false);
        mButtonAC_CoolPtr->setSelected(false);
        mButtonAC_WinPtr->setSelected(false);
        mButtonAC_waterPtr->setSelected(false);
    }
    return false;
}

static bool onButtonClick_ButtonAC_auto(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAC_auto !!!\n");
    mButtonAC_autoPtr->setSelected(true);
    mButtonAC_HeatPtr->setSelected(false);
    mButtonAC_CoolPtr->setSelected(false);
    mButtonAC_WinPtr->setSelected(false);
    mButtonAC_waterPtr->setSelected(false);
   if(pButton->isSelected()){
	   yh_air_setmode(41, 0);
   }
    return false;
}

static bool onButtonClick_ButtonAC_water(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAC_water !!!\n");
    mButtonAC_autoPtr->setSelected(false);
	mButtonAC_HeatPtr->setSelected(false);
	mButtonAC_CoolPtr->setSelected(false);
	mButtonAC_WinPtr->setSelected(false);
	mButtonAC_waterPtr->setSelected(true);
   if(pButton->isSelected()){
	   yh_air_setmode(41, 2);
   }
    return false;
}

static bool onButtonClick_ButtonAC_Cool(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAC_Cool !!!\n");
    mButtonAC_autoPtr->setSelected(false);
	mButtonAC_HeatPtr->setSelected(false);
	mButtonAC_CoolPtr->setSelected(true);
	mButtonAC_WinPtr->setSelected(false);
	mButtonAC_waterPtr->setSelected(false);
   if(pButton->isSelected()){
	   yh_air_setmode(41, 1);
   }
    return false;
}

static bool onButtonClick_ButtonAC_Heat(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAC_Heat !!!\n");

    mButtonAC_autoPtr->setSelected(false);
	mButtonAC_HeatPtr->setSelected(true);
	mButtonAC_CoolPtr->setSelected(false);
	mButtonAC_WinPtr->setSelected(false);
	mButtonAC_waterPtr->setSelected(false);
   if(pButton->isSelected()){
	   yh_air_setmode(41, 4);
   }
    return false;
}

static bool onButtonClick_ButtonAC_Win(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonAC_Win !!!\n");

    mButtonAC_autoPtr->setSelected(false);
	mButtonAC_HeatPtr->setSelected(false);
	mButtonAC_CoolPtr->setSelected(false);
	mButtonAC_WinPtr->setSelected(true);
	mButtonAC_waterPtr->setSelected(false);
   if(pButton->isSelected()){
	   yh_air_setmode(41, 3);
   }
    return false;
}
